layui.use(['form','layer','tree', 'util'],function(){
    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer,
            tree = layui.tree

            ,util = layui.util,
        $ = layui.jquery;

    var  treeData;
    var functional=""; // 所有选中的 资源id


    var menuTree ;
    //
    $.get("../../role/getmenu",{
        id : 1

    },function(result){
        debugger;
        treeData =result.data;

        //基本演示
        menuTree =   tree.render({
            elem: '#test12'
            ,data: treeData
            ,showCheckbox: true  //是否显示复选框
            ,id: 'demoId1'
            ,isJump: false //是否允许点击节点时弹出新窗口跳转
            // ,spread: true
           ,spread: true //展开
            // ,accordion:true
            ,click: function(obj){
                debugger
                var data = obj.data;  //获取当前点击的节点数据
                  layer.msg('状态：'+ obj.state + '<br>节点数据：' + JSON.stringify(data));
            }
        });



        //
        $.get("../../role/getmenu-by-role",{
            roleId : $("#roleId").val()
        },function(res){
            var ids = res.data;
            var sourceIds = new Array();
            for(var i in ids){
                var id = ids[i].permissionId;
                if( 4 < id ){
                    //  时间紧迫  父节点 忽略 不选中
                    sourceIds[i] = id;
                }
            }

            tree.setChecked('demoId1', sourceIds); //批量勾选 id 为 2、3 的节点t
            $('input[type="checkbox"]').attr("disabled",false);
            $(".layui-tree-txt").removeClass("layui-disabled");
            $(".layui-form-checkbox").removeClass("layui-disabled");
        })
    });


   // menuTree.setChecked('demoId1', [17, 18,14]); //批量勾选 id 为 2、3 的节点

    // layui.form.render();//重新渲染 可以解决多种没有显示的情况


    form.on("submit(addUser)",function(data){
        //获得选中的节点
        var checkData = menuTree.getChecked('demoId');
        var nodeIds = new Array();
        nodeIds = getCheckedId(checkData);
        var permList = nodeIds;
        console.log("permList:"+permList);
        debugger
        // 获取选中节点的id
        function getCheckedId(jsonObj) {
            var id = "";
            $.each(jsonObj, function (index, item) {
                if (id != "") {
                    id = id + "," + item.id;
                }
                else {
                    id = item.id;
                }
                var i = getCheckedId(item.children);
                if (i != "") {
                    id = id + "," + i;
                }
            });
            return id;
        }
        if (permList.length == 0){
            console.log("数组为空")
            var index = top.layer.msg(' 请选择菜单',{time:2000} ,function() {
                //回调
            });

            return false;

        }else {
            console.log("数组不为空")
        }

        //弹出loading
        var index = top.layer.msg('数据提交中，请稍候',{icon: 16,time:false,shade:0.8});

        // 实际使用时的提交信息
        $.post("../../role/roleauthor",{
            roleId : $("#roleId").val(),
            ids:permList
        },function(res){
            // 请求成功 刷新页面
            setTimeout(function( ){
                top.layer.close(index);
                top.layer.msg( res.msg);
                layer.closeAll("iframe");
                //刷新父页面
                parent.location.reload();
            },500);
        })

        ///



        ///


        return false;
    });


    //

    //格式化时间
    function filterTime(val){
        if(val < 10){
            return "0" + val;
        }else{
            return val;
        }
    }
    //定时发布
    var time = new Date();
    var submitTime = time.getFullYear()+'-'+filterTime(time.getMonth()+1)+'-'+filterTime(time.getDate())+' '+filterTime(time.getHours())+':'+filterTime(time.getMinutes())+':'+filterTime(time.getSeconds());

})